<?

//Data loading
$title='REPORT DEBIT NOTE PERFORMANCE';
$curr = GetParam("curr","");

$perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';
$TableName = "ais_memorial";
$DEFINE_FONT=0;
$pdf=new PDFReport("L","mm","Letter");
$pdf->AddPage();
$Result = ReportList ("", "", "");
if ($Result) $pdf->ReportTable($Result);
$pdf->Output();



function ReportList ($Space, $Index, $ParentID) {
            global $RS,$Index,$TableName,$curr;
            $DBConnection   = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
            if (!$Index) $Index = 0;

//SELECT * FROM ais_memorial WHERE ktrasl='000' AND (1) AND urut=999 AND nobkt like 'ND%' ORDER BY tglbkt DESC, nobkt DESC
//SELECT * FROM ais_memorial WHERE nobkt ='ND1005005748' AND ktrasl='000' AND urut<>999 ORDER BY urut  

/*
$SQL = " SELECT * FROM `$TableName` as a
         LEFT JOIN tbljoborder as b ON a.jo_id = b.jo_id
         LEFT JOIN tblservices as c ON a.services_id = c.services_id 
       ";        
*/

/*

$SQL = " SELECT * FROM `$TableName` as mem
         LEFT JOIN tbljoactivity as a ON mem.services_id = a.services_id
         LEFT JOIN tbljoborder as b ON a.jo_id = b.jo_id AND mem.jo_code = b.jo_code
         LEFT JOIN tblservices as c ON a.services_id = c.services_id 
       "; 
  
*/


/*

WHERE mem.services_id IS NOT NULL AND a.jo_date >= '2010-05-03' AND a.jo_date <= '2010-05-31' AND b.curr like '%USD%'
*/


$SQL = " SELECT * FROM ".$TableName." as mem 
		 INNER JOIN tbljoborder as a ON mem.jo_code = a.jo_code
		 INNER JOIN tbljoactivity as b ON (mem.services_id = b.services_id AND a.jo_id = b.jo_id)
       ";           

    if (GetParam("fromDate",""))
    {
        list($day1,$month1,$year1) = explode("/",GetParam("fromDate",""));
        $FDate = "$year1-$month1-$day1";
    }

    if (GetParam("toDate",""))
    {
        list($day2,$month2,$year2) = explode("/",GetParam("toDate",""));
        $TDate = "$year2-$month2-$day2";
    }


    if ($FDate)
        if (strlen($WHERE)) $WHERE .= " AND a.jo_date >= '".$FDate."' ";
        else $WHERE .= " a.jo_date >= '".$FDate."' ";

    if ($TDate)
        if (strlen($WHERE)) $WHERE .= " AND a.jo_date <= '".$TDate."' ";
        else $WHERE .= " a.jo_date <= '".$TDate."' ";

  
    if (strlen($WHERE)) $WHERE = " WHERE mem.services_id IS NOT NULL AND b.curr like '%".$curr."%' AND mem.nobkt like 'ND%' AND ".$WHERE;
        else $WHERE = " WHERE mem.services_id IS NOT NULL AND b.curr like '%".$curr."%' AND mem.nobkt like 'ND%' ";


    $SQL = $SQL.$WHERE;


    $SQL .= " ORDER BY a.jo_code DESC ";



                           //die($SQL);
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
    return $MyResult;
}




class PDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT,$curr;
         
        //Initial Date 
        $FDate = GetParam("fromDate","");		
		$TDate = GetParam("toDate","");		
		$SystemDate = date("d/m/Y");		
		//End Initial Date
        
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        $this->SetFont('arialn','',12);
                $this->Cell(195,5,$perush,0,1,'C',0);
                $this->Cell(195,5,$title,0,1,'C',0);
                
                $this->Cell(195,5,"REPORT FROM: ".GetParam("fromDate","")." TO: ".$TDate,0,1,'C',0);                
                $this->Cell(195,5,"CURRENCY: ".$curr,0,1,'C',0);
                $this->Cell(195,5,$page." ".$this->PageNo(),0,1,'R',0);
                $this->Line(10,30,205,30);
                $this->Ln();
                $this->Ln();
                //Header

                //Column widths               
                $w=array(23,25,25,25,25,20,45,40,20);
                $this->SetFont('arialn','',8);

                $this->Cell($w[0],7,"JOB ORDER NO",1,0,'C',0);
                $this->Cell($w[1],7,"JOB ORDER DATE",1,0,'C',0);
                $this->Cell($w[2],7,"TOTAL JO AMOUNT",1,0,'C',0);
                $this->Cell($w[3],7,"ND NO.",1,0,'C',0);
                $this->Cell($w[4],7,"ND DATE",1,0,'C',0);                
                $this->Cell($w[5],7,"ND AMOUNT",1,0,'C',0);
                $this->Cell($w[6],7,"DESCRIPTION",1,0,'C',0);
                $this->Cell($w[7],7,"REMARKS",1,0,'C',0);
                $this->Cell($w[8],7,"ND Status",1,0,'C',0);

                $this->Ln();
    }

    function ReportTable($Result) {
        //Column widths
        $w=array(23,25,25,25,25,20,45,40,20);
        
        global $curr;
        
        //Data
        foreach($Result as $row)
        {

                $no++;
                                
                //Date Parse
                list($year,$month,$day) = explode("-",$row["jo_date"]);
                $FDate = "$day-$month-$year";                
                
                list($year2,$month2,$day2) = explode("-",$row["tglbkt"]);
                $FDate_2 = "$day2-$month2-$year2";                
                //End Date Parse                
                
                $this->Cell($w[0],6,$row["jo_code"],'LR',0,'C');
                                
                $this->Cell($w[1],6,$FDate,'LR',0,'C');
                                
                if($curr == "USD")
                {
					$this->Cell($w[2],6,number_format($row["total_amount_usd"],2),'LR',0,'R');					
					$TotalAmount+= $row["total_amount_usd"];
				}
                
                else if($curr == "IDR")
                {
					$this->Cell($w[2],6,number_format($row["total_amount"],1),'LR',0,'R');
					$TotalAmount+= $row["total_amount"];
				}
                
                $this->Cell($w[3],6,$row["nobkt"],'LR',0,'C');

                $this->Cell($w[4],6,$FDate_2,'LR',0,'C');
                
                $this->Cell($w[5],6,number_format($row["jumlah"],2),'LR',0,'R');                
                
                $NDAmount+= $row["jumlah"];
                
                $this->Cell($w[6],6,$row["uraian"],'LR');
                
                if($curr == "USD")
                {
                                    
                  if($row["joactivity_remain_usd"] > 0)
                  {
                     $this->Cell($w[7],6,"Remain : ".number_format($row["joactivity_remain_usd"],2),'LR',0,'C');
                  }
                  
                  else
                  {
					 $this->Cell($w[7],6,"",'LR',0,'C');
				  }
                  
                }
                
                else if($curr == "IDR")
                {
                                                                        
                  if($row["joactivity_remain_idr"] > 0)
                  {
                     $this->Cell($w[7],6,"Remain : ".number_format($row["joactivity_remain_idr"],2),'LR',0,'C');
                  }
                  
                  else
                  {
					 $this->Cell($w[7],6,"",'LR',0,'C');
				  }
				  
                }
                
                $this->Cell($w[8],6,$row["nk_cancel_status"],'LR',0,'C');
                
                $this->Ln();
        }


            $this->Cell($w[0]+$w[1],6,"TOTAL:",'LTR');
            $this->Cell($w[2],6,number_format($TotalAmount,2),'LTR',0,'R');
            $this->Cell($w[3]+$w[4],6,"",'LTR',0,'R');
            $this->Cell($w[5],6,number_format($NDAmount,2),'LTR',0,'R');
            $this->Cell($w[6]+$w[7]+$w[8],6,"",'LTR',0,'R');
            $this->Ln();

        //Closure line
        //$this->Cell(array_sum($w),0,'','T');
    }

    function Footer() {
        //Closure line
        $this->Cell(248,0,'','T');
    }

}


?>
